package org.chen.concurrent.springbootmybatisconcurrent.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;

import java.util.HashSet;
import java.util.Set;

@Slf4j
@Configuration
public class RedisConfig {

    @Value("${spring.redis.cluster.nodes}")
    private String clusterNodes;

    public JedisCluster jedisCluster() {
        String[] nodes = clusterNodes.split(",");
        Set<HostAndPort> serverNodes = new HashSet<>();
        for (String node : nodes) {
            String[] hostAndPort = node.split(":");
            serverNodes.add(new HostAndPort(hostAndPort[0].trim(), Integer.valueOf(hostAndPort[1].trim())));
        }
        return new JedisCluster(serverNodes);
    }


}
